package com.ruicar.afs.cloud.afscase.paramconfmanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.paramconfmanagement.condition.CaseConfParamQueryCondition;
import com.ruicar.afs.cloud.afscase.paramconfmanagement.entity.CaseConfParam;
import com.ruicar.afs.cloud.afscase.paramconfmanagement.service.CaseConfParamService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;

/**
 * @version V1.0
 * @author: bh.xia
 * @date: 2020/6/4 9:42
 * @description:
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api("参数配置管理")
@RequestMapping("/caseConfParam")
public class CaseConfParamController {

    private CaseConfParamService caseConfParamService;

    /**
     * 获取参数配置列表
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/queryCaseConfParamList")
    @ApiOperation(value = "多条件分页获取参数配置数据列表")
    public IResponse<IPage<CaseConfParam>> queryCaseConfParamList(@ModelAttribute CaseConfParamQueryCondition condition) {
        Page<CaseConfParam> page = caseConfParamService.page(new Page(condition.getPageNumber(), condition.getPageSize()), Wrappers.<CaseConfParam>query().lambda()
                .eq(StringUtils.isNotEmpty(condition.getParamType()), CaseConfParam::getParamType, condition.getParamType())
                .eq(StringUtils.isNotEmpty(condition.getParamStatus()), CaseConfParam::getParamStatus, condition.getParamStatus())
                .eq(StringUtils.isNotEmpty(condition.getParamExplain()), CaseConfParam::getParamExplain, condition.getParamExplain())
                .eq(StringUtils.isNotEmpty(condition.getParamLogo()), CaseConfParam::getParamLogo, condition.getParamLogo()));
        return IResponse.success(page);
    }

    /**
     * 修改参数配置数据
     *
     * @param caseConfParam
     * @return
     */
    @PostMapping(value = "/editCaseConfParam")
    @ApiOperation(value = "修改参数配置数据")
    @Transactional
    public IResponse<Boolean> editCaseConfParam(@ModelAttribute CaseConfParam caseConfParam) {

        caseConfParamService.updateById(caseConfParam);
        return new IResponse<Boolean>().setMsg("修改参数配置数据成功！");
    }

    /**
     * 根据参数id获取参数
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getCaseConfParam/{id}")
    @ApiOperation(value = "根据参数id获取参数")
    public IResponse<CaseConfParam> getCaseConfParam(@PathVariable("id") BigInteger id) {

        CaseConfParam caseConfParam = caseConfParamService.getOne(Wrappers.<CaseConfParam>query().lambda()
                .eq(StringUtils.isNotEmpty((CharSequence) id), CaseConfParam::getId, id));
        return IResponse.success(caseConfParam);
    }

    /**
     * 修改参数配置数据
     *
     * @param caseConfParam
     * @return
     */
    @PostMapping(value = "/saveCaseConfParam")
    @ApiOperation(value = "修改参数配置数据")
    @Transactional
    public IResponse<Boolean> saveCaseConfParam(@RequestBody CaseConfParam caseConfParam) {
        return caseConfParamService.saveParam(caseConfParam);
    }
}
